<?php

class MemberExtension extends DataExtension
{
    public static $db = array(
        "Username" => "Varchar(18)",
        "Gender" => "Enum(array('male', 'female'))",
        "AccountStatus" => "Enum(array('pending', 'active', 'deactivated'))"
    );

    public static $indexes = array(
        "Username_ID" => array("type" => "index", "value" => "Username, ID")
    );

    public function canLogIn($result) {
        // Short-circuit the result upon failure, no further checks needed.
        if (!$result->valid()) return $result;

        if($this->owner->AccountStatus == Globals::STATUS_ACTIVE) {
            // Account is fine. Do nothing here.
        } elseif ($this->owner->AccountStatus == Globals::STATUS_DEACTIVATED) {
            $result->error(
                _t(
                    'MemberExtension.ERROR_DEACTIVATED',
                    'Your account is deactivated.'
                )
            );
        } else {
            // Default: Account status is pending.
            $result->error(
                _t(
                    'MemberExtension.ERROR_PENDING',
                    'Your account is not activated yet.'
                )
            );
        }

        return $result;
    }

    static function GetGenderField($showAny = false)
    {
        $arr = array(
            "" => "(Any)",
            "male" => "Male",
            "female" => "Female"
        );

        if (!$showAny) {
            unset($arr[""]);
        }

        $field = new DropdownField
        (
            "Gender",
            _t("General.GENDER", "Gender"),
            $arr
        );

        return $field;
    }

    static function GetAccountStatusField($showAny = false)
    {
        $arr = array(
            "" => "(Any)",
            "active" => "Active",
            "pending" => "Pending Approve",
            "deactivated" => "Deactivated"
        );

        if (!$showAny) {
            unset($arr[""]);
        }

        $field = new DropdownField
        (
            "AccountStatus",
            _t("General.ACCOUNT_STATUS", "Status"),
            $arr
        );

        return $field;
    }

    static function GetFormattedAccountStatus($status)
    {
        switch ($status) {
            case Globals::STATUS_ACTIVE:
                return "Active";
            case Globals::STATUS_PENDING:
                return "Pending Approve";
            case Globals::STATUS_DEACTIVATED:
                return "Deactivated";
            default:
                return "-";
        }
    }
}